package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreOrderInfoBo;
import com.ruoyi.system.domain.vo.PStoreOrderInfoVo;
import com.ruoyi.system.domain.PStoreOrderInfo;
import com.ruoyi.system.mapper.PStoreOrderInfoMapper;
import com.ruoyi.system.service.IPStoreOrderInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单购物详情Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@RequiredArgsConstructor
@Service
public class PStoreOrderInfoServiceImpl implements IPStoreOrderInfoService {

    private final PStoreOrderInfoMapper baseMapper;

    /**
     * 查询订单购物详情
     */
    @Override
    public PStoreOrderInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单购物详情列表
     */
    @Override
    public TableDataInfo<PStoreOrderInfoVo> queryPageList(PStoreOrderInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreOrderInfo> lqw = buildQueryWrapper(bo);
        Page<PStoreOrderInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单购物详情列表
     */
    @Override
    public List<PStoreOrderInfoVo> queryList(PStoreOrderInfoBo bo) {
        LambdaQueryWrapper<PStoreOrderInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreOrderInfo> buildQueryWrapper(PStoreOrderInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreOrderInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, PStoreOrderInfo::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductId() != null, PStoreOrderInfo::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), PStoreOrderInfo::getInfo, bo.getInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getUnique()), PStoreOrderInfo::getUnique, bo.getUnique());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), PStoreOrderInfo::getOrderNo, bo.getOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), PStoreOrderInfo::getProductName, bo.getProductName());
        lqw.eq(bo.getAttrValueId() != null, PStoreOrderInfo::getAttrValueId, bo.getAttrValueId());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), PStoreOrderInfo::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getSku()), PStoreOrderInfo::getSku, bo.getSku());
        lqw.eq(bo.getPrice() != null, PStoreOrderInfo::getPrice, bo.getPrice());
        lqw.eq(bo.getPayNum() != null, PStoreOrderInfo::getPayNum, bo.getPayNum());
        lqw.eq(bo.getWeight() != null, PStoreOrderInfo::getWeight, bo.getWeight());
        lqw.eq(bo.getVolume() != null, PStoreOrderInfo::getVolume, bo.getVolume());
        lqw.eq(bo.getGiveIntegral() != null, PStoreOrderInfo::getGiveIntegral, bo.getGiveIntegral());
        lqw.eq(bo.getIsReply() != null, PStoreOrderInfo::getIsReply, bo.getIsReply());
        lqw.eq(bo.getIsSub() != null, PStoreOrderInfo::getIsSub, bo.getIsSub());
        lqw.eq(bo.getVipPrice() != null, PStoreOrderInfo::getVipPrice, bo.getVipPrice());
        lqw.eq(bo.getProductType() != null, PStoreOrderInfo::getProductType, bo.getProductType());
        return lqw;
    }

    /**
     * 新增订单购物详情
     */
    @Override
    public Boolean insertByBo(PStoreOrderInfoBo bo) {
        PStoreOrderInfo add = BeanUtil.toBean(bo, PStoreOrderInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单购物详情
     */
    @Override
    public Boolean updateByBo(PStoreOrderInfoBo bo) {
        PStoreOrderInfo update = BeanUtil.toBean(bo, PStoreOrderInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreOrderInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单购物详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
